/**
 * Copyright (c) iwindplus Technologies Co., Ltd.2024-2030, All rights reserved.
 */

package com.iwindplus.boot.web.thread;

import com.alibaba.ttl.TtlCallable;
import com.alibaba.ttl.TtlRunnable;
import java.util.concurrent.Callable;
import java.util.concurrent.Future;
import org.springframework.scheduling.concurrent.ThreadPoolTaskScheduler;

/**
 * 定时任务线程池（解决线程变量共享）.
 *
 * @author zengdegui
 * @since 2023/08/29 21:28
 */
public class TtlThreadPoolTaskScheduler extends ThreadPoolTaskScheduler {

    @Override
    public void execute(Runnable task) {
        Runnable runnable = TtlThreadPoolTaskExecutor.wrap(TtlRunnable.get(task));
        super.execute(runnable);
    }

    @Override
    public <T> Future<T> submit(Callable<T> task) {
        Callable<T> callable = TtlThreadPoolTaskExecutor.wrap(TtlCallable.get(task));
        return super.submit(callable);
    }

    @Override
    public Future<?> submit(Runnable task) {
        Runnable runnable = TtlThreadPoolTaskExecutor.wrap(TtlRunnable.get(task));
        return super.submit(runnable);
    }

    @Override
    protected void cancelRemainingTask(Runnable task) {
        Runnable runnable = TtlThreadPoolTaskExecutor.wrap(TtlRunnable.get(task));
        super.cancelRemainingTask(runnable);
    }
}
